//
//  RBRegisterController.m
//  StoryToy
//
//  Created by baxiang on 2017/10/16.
//  Copyright © 2017年 roobo. All rights reserved.
//

#import "RBRegisterController.h"
#import "UIImage+RBExtension.h"
#import "RTLineTextField.h"
@interface RBRegisterController ()<UITextFieldDelegate>
@property (nonatomic, weak) RTLineTextField * accoutTxtV;
@property (nonatomic, weak) RTLineTextField *psdTxtV;
@property (nonatomic, weak) RTLineTextField * verifyCodeTxtV;
@property (nonatomic, weak) UILabel * agreementLabel;
@property (nonatomic, weak) UIButton * registBtn;
@property (nonatomic, weak) UIButton *verifyBtn;
@property (nonatomic, strong) NSMutableAttributedString *attributeString;
@end
@implementation RBRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setHidden:NO];
    self.title = @"快速注册";
    
    RTLineTextField *accoutTxtV = [RTLineTextField new];
    accoutTxtV.placeholder = @"请输入您的手机号";
    accoutTxtV.keyboardType = UIKeyboardTypePhonePad;
    accoutTxtV.returnKeyType = UIReturnKeyNext;
    accoutTxtV.textColor = UIColorHex(0x576166);
    accoutTxtV.backgroundColor = [UIColor clearColor];
    accoutTxtV.keyboardType = UIKeyboardTypePhonePad;
    accoutTxtV.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:accoutTxtV];
    _accoutTxtV = accoutTxtV;
   
    [accoutTxtV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.height.mas_equalTo(45);
        make.top.mas_equalTo(90);
    }];
    
    RTLineTextField * psdTxtV = [RTLineTextField new];
    psdTxtV.placeholder =  @"请输入6-20位密码";
    psdTxtV.returnKeyType = UIReturnKeyNext;
    psdTxtV.secureTextEntry = YES;
    psdTxtV.keyboardType = UIKeyboardTypeASCIICapable;
    psdTxtV.textColor =  UIColorHex(0x576166);;
    psdTxtV.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:psdTxtV];
    _psdTxtV = psdTxtV;
    [psdTxtV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accoutTxtV.mas_left);
        make.right.mas_equalTo(accoutTxtV.mas_right);
        make.height.mas_equalTo(accoutTxtV.mas_height);
        make.top.mas_equalTo(accoutTxtV.mas_bottom).offset(30);
    }];
    [psdTxtV.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if ([x containsString:@" "]) {
            //[RBToastView showWithText:@"密码不能包含空格" inView:self.view];
        }
    }];

    UIButton *secBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [secBtn setImage:[UIImage imageNamed:@"icon_eyes_close"] forState:UIControlStateNormal];
    [secBtn setImage:[UIImage imageNamed:@"icon_eyes_open"] forState:UIControlStateSelected];
    secBtn.frame = CGRectMake(0, 0, 26, 13);
    psdTxtV.rightView = secBtn;
    psdTxtV.rightViewMode = UITextFieldViewModeAlways;
    @weakify(self);
    [[secBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * btn) {
        @strongify(self);
        [btn setSelected:!btn.isSelected];
        self.psdTxtV.secureTextEntry = !btn.isSelected;
    }];
    
    UIButton *verifyBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:verifyBtn];
    verifyBtn.layer.masksToBounds = YES;
    verifyBtn.layer.cornerRadius = 35 *0.5;
    [verifyBtn setBackgroundImage:[UIImage imageWithColor:RTColorForBackground] forState:UIControlStateNormal];
    [verifyBtn setBackgroundImage:[UIImage imageWithColor: UIColorHex(0xc0c0c1)] forState:UIControlStateDisabled];
    [verifyBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    verifyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [verifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [verifyBtn addTarget:self action:@selector(sendVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    _verifyBtn = verifyBtn;
    
    RTLineTextField *verifyCodeTxtV = [RTLineTextField new];
    verifyCodeTxtV.placeholder =  @"4位短信验证码";
    verifyCodeTxtV.keyboardType = UIKeyboardTypePhonePad;
    verifyCodeTxtV.textColor = UIColorHex(576166);;
    verifyCodeTxtV.font = [UIFont systemFontOfSize:13];
    _verifyCodeTxtV = verifyCodeTxtV;
    [self.view addSubview:verifyCodeTxtV];
    
    [verifyCodeTxtV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(psdTxtV.mas_bottom).offset(30);
        make.left.mas_equalTo(accoutTxtV.mas_left);
        make.right.mas_equalTo(verifyBtn.mas_left).offset(-10);
        make.height.mas_equalTo(45);
    }];
    
    [verifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-50);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(35);
        make.bottom.mas_equalTo(verifyCodeTxtV.mas_bottom);
    }];
    
    UIButton*registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:registBtn];
    [registBtn setTitle:@"快速注册" forState:UIControlStateNormal];
    registBtn.backgroundColor  = RTColorForBackground;
    registBtn.layer.cornerRadius = 45*0.5;
    registBtn.layer.masksToBounds = YES;
    [registBtn addTarget:self action:@selector(sendRegistRequest) forControlEvents:UIControlEventTouchUpInside];
    [registBtn setBackgroundImage:[UIImage imageWithColor:RTColorForBackground] forState:UIControlStateNormal];
    [registBtn setBackgroundImage:[UIImage imageWithColor: UIColorHex(0xc0c0c1)] forState:UIControlStateDisabled];
    [self.view addSubview:registBtn];
    _registBtn = registBtn;
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accoutTxtV.mas_left);
        make.right.mas_equalTo(accoutTxtV.mas_right);
        make.height.mas_equalTo(45);
        make.top.mas_equalTo(_verifyCodeTxtV.mas_bottom).offset(30);
    }];
    
    
    RAC(self.registBtn,enabled) = [RACSignal combineLatest:@[accoutTxtV.rac_textSignal,psdTxtV.rac_textSignal,verifyCodeTxtV.rac_textSignal] reduce:^id (NSString *phone,NSString*password,NSString*code){
        return @(phone.length>0 && password.length>5&&code.length>0);
    }];
    [accoutTxtV.rac_textSignal subscribeNext:^(NSString * phone) {
        if (phone.length>0) {
            self.verifyBtn.enabled = YES;
        }else{
            self.verifyBtn.enabled = NO;
        }
        if (phone.length==11) {
            [self checkPhoneIsRegister:phone];
        }
    }];
}

-(void)sendVerifyCode
{
    self.verifyBtn.enabled = NO;
    [RBUserApi sendCode:self.accoutTxtV.text type:RBSendCodeTypeRegister completionBlock:^(BOOL isSend, NSError *error) {
        if (isSend) {
            NSLog(@"验证码已发送");
            //[RTUserMgr showWithText:@"验证码已发送" inView:self.view ];
            [self countDownHandle];
        }else{
            NSLog(@"验证码发送失败");
            self.verifyBtn.enabled = YES;
            //[RTUserMgr showWithError:error inView:self.view];
        }
    }];
}


-(void)countDownHandle
{
    __block  NSInteger timeOut  = 60;
    RACSignal *racSignal  = [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] timeout:60 onScheduler:[RACScheduler currentScheduler]];
    [racSignal subscribeNext:^(id x) {
        timeOut--;
        [self.verifyBtn setTitle:[NSString stringWithFormat:@"%lds后重新发送",timeOut] forState:UIControlStateNormal];
        self.verifyBtn.enabled = NO;
    }error:^(NSError *error) {
        [self.verifyBtn setTitle:@"重新发送" forState:UIControlStateNormal];
        self.verifyBtn.enabled = YES;
    }];
}

- (void)checkPhoneIsRegister:(NSString *)phoneText{
  
    @weakify(self);
    [RBUserApi isRegist:phoneText block:^(BOOL isRegist, NSError * error) {
        @strongify(self);
        if (isRegist) {
            //            [RTUserMgr showWithText:@"手机号码已注册" inView:self.view ];
        }
    }];
}


#pragma mark - action: 发送注册请求
- (void)sendRegistRequest{
    NSString *phone = self.accoutTxtV.text;
    //    NSString *pushToken = [RTPermissionManager fetchDeviceToken];
    //     __block RBToastView *toastView = [RBToastView showLoadingInView:self.view];
    [RBUserApi regist:phone password:self.psdTxtV.text code:self.verifyCodeTxtV.text nickName:self.accoutTxtV.text pushToken:@"" block:^(RBUserModel * user, NSError *  error) {
        //[toastView hideAnimated:YES];
        //[RTUserMgr savePhoneNum:phone];
        if (error){
            NSLog(@"注册失败");
            //[RTUserMgr showWithError:error inView:self.view];
            return;
        }else {
            NSLog(@"注册成功");
        }
        //[RTUserMgr saveLoginUser:user switchRootVC:YES];
    }];
}

@end
